package com.simplesoftwares.client.library.mywidgets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.ListBox;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.libservice.GenricService;
import com.simplesoftwares.client.library.libservice.GenricServiceAsync;

public class ObjectListBox<T> extends ListBox implements HasValue<String>{
	protected List<T> items;
	
	private final GenricServiceAsync genricservice = GWT
			.create(GenricService.class);

	public ObjectListBox()
	{
		super();
		items=new ArrayList<T>();
		
	}
	
	public ObjectListBox(List<T> items) {
		super();
		this.items = items;
		setListItems(items);
	}
	
	public void setListItems(List<T>vec)
	{
		removeAllItems();
		
		addItem("--SELECT--");
		items=vec;
		for(int i=0;i<items.size();i++)
			addItem(items.get(i).toString());
	}
	
	
	public void removeAllItems()
	{
		items=new ArrayList<T>();
		int count=this.getItemCount()-1;
		
		for(int i=count;i>=0;i--)
		{
		    
			removeItem(i);
		    
		    
		}
		
		
		
	}
	
	
	public T getSelectedItem()
	{
		if(super.getSelectedIndex()==0)
			return null;
		else
		  return items.get(getSelectedIndex()-1);
	}
	
	public void MakeLive(MyQuerry query)
	{
		
		genricservice.getSearchResult(query,new AsyncCallback<ArrayList<SuperModel>>()
			{

					@Override
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
						
					}

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(ArrayList<SuperModel> result)
					{
					
						items=new ArrayList<T>();
						items.addAll((Collection<? extends T>) result);
					    setListItems(items);
					  
						
					}
		});
	}

	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<String> handler) {
		// TODO Auto-generated method stub
		return this.addValueChangeHandler(handler);
	}

	@Override
	public String getValue() 
	{
		if(super.getSelectedIndex()==0)
			return null;
		else
		  return items.get(super.getSelectedIndex()-1).toString();
	}

	@Override
	public void setValue(String value) {
		int count = getItemCount();
		String item=value.toString();
		for(int i=0;i<count;i++)
		{
			if(item.trim().equals(getItemText(i).trim()))
			{
				setSelectedIndex(i);
				break;
			}
		}
		
	}

	@Override
	public void setValue(String value, boolean fireEvents) {
		
		
	}

	public List<T> getItems() {
		// TODO Auto-generated method stub
		if(items!=null)
			return items;
		return null;
	}

	public void setItems(List<T> items) {
		this.items = items;
	}
			
		
		
}
	
	

